package com.ruoyi.human.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.Participant;
import com.ruoyi.human.service.IParticipantService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 参与者基本信息Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/participant")
public class ParticipantController extends BaseController
{
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private RemoteFileService remoteFileService;
    /**
     * 查询参与者基本信息列表
     */
    @RequiresPermissions("human:participant:list")
    @GetMapping("/list")
    public TableDataInfo list(Participant participant)
    {
        startPage();
        List<Participant> list = participantService.selectParticipantList(participant);
        return getDataTable(list);
    }

    /**
     * 导出参与者基本信息列表
     */
    @RequiresPermissions("human:participant:export")
    @Log(title = "参与者基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Participant participant)
    {
        List<Participant> list = participantService.selectParticipantList(participant);
        ExcelUtil<Participant> util = new ExcelUtil<Participant>(Participant.class);
        util.exportExcel(response, list, "参与者基本信息数据");
    }

    /**
     * 获取参与者基本信息详细信息
     */
    @RequiresPermissions("human:participant:query")
    @GetMapping(value = "/{participantId}")
    public AjaxResult getInfo(@PathVariable("participantId") Long participantId)
    {
        return success(participantService.selectParticipantByParticipantId(participantId));
    }


    /**
     * 新增参与者基本信息
     */
    @RequiresPermissions("human:participant:add")
    @Log(title = "参与者基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Participant participant)
    {
        participant.setCreatedBy(SecurityUtils.getUsername());
        participant.setCreatedAt(new Date());
        return toAjax(participantService.insertParticipant(participant));
    }

    /**
     * 修改参与者基本信息
     */
    @RequiresPermissions("human:participant:edit")
    @Log(title = "参与者基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Participant participant)
    {
        participant.setUpdatedBy(SecurityUtils.getUsername());
        participant.setUpdatedAt(new Date());
        participant.setVersion(participant.getVersion()+1);
        return toAjax(participantService.updateParticipant(participant));
    }

    /**
     * 删除参与者基本信息
     */
    @RequiresPermissions("human:participant:remove")
    @Log(title = "参与者基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{participantIds}")
    public AjaxResult remove(@PathVariable Long[] participantIds)
    {
        for (Long id:participantIds){
            Participant participant=participantService.selectParticipantByParticipantId(id);
            participant.setDeletedAt(new Date());
            participant.setDeletedBy(SecurityUtils.getUsername());
            participant.setDeletedFlag(1);
            participantService.updateParticipant(participant);
        }
        return toAjax(true);
    }

    /**
     * 图片上传
     */
    @Log(title = "用户头像", businessType = BusinessType.INSERT)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file)
    {
        if (!file.isEmpty())
        {
            String extension = FileTypeUtils.getExtension(file);
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION))
            {
                return error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return error("文件服务异常，请联系管理员");
            }

            String url = fileResult.getData().getUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
    @GetMapping("/getParticipants")
    public AjaxResult getParticipants()
    {
        Participant participantQuery = new Participant();
        participantQuery.setDeletedFlag(0);
        List<Participant> list = participantService.selectParticipantList(participantQuery);
        return success(list);
    }
}
